from Tkinter import *
from modsfd import snake
from modsfd import food
from time import sleep


class App(object):
	def __init__(self):
		self.master = Tk()

		self.ledge = 0
		self.redge = 300
		self.tedge = 0
		self.bedge = 400
		self.winNum = 3
		
		self.direction = 'd'
		self.dirc = 'wsad'
		self.dirn = {'w':-1, 'd':-2, 's':1, 'a':2}
		
		
		self.toolBar = Frame(self.master, width=40, bg='gray')
		self.toolBar.pack(fill=Y, expand=False, side = LEFT)
		
		self.var = StringVar()
		self.scoreLabel = Label(self.toolBar, bg='yellow',textvariable = self.var)
		self.scoreLabel.pack(fill=X)
		
		self.butt = Button(self.toolBar, bg='Green', text="Start", command=self.startG)
		self.butt.pack(fill=X)
		
		self.pauseButt = Button(self.toolBar, foreground='white', bg='Blue', text='Pause', command=self.pauseG)
		self.pauseButt.pack(fill=X)

		self.w = Canvas(self.master, width=self.redge, height=self.bedge, background="white")

		self.w.pack(side = RIGHT)
		self.swl = False
		self.tail = [0,0]

		self.sk = snake.Snake()
		self.pie = food.food()
		self.master.bind("<Key>", self.key)
		#self.w.focus_set()
		
		self.master.mainloop()
	

	def startG(self):
		self.w.create_rectangle(0,0, 300,400, fill="white")
		self.pauseFlag = False		
		self.score = -1
		self.var.set('0')
		self.tail = [0,0]
		self.swl = False
		self.sk = snake.Snake()
		self.pie = food.food()
		self.master.after(10, self.task)

	def pauseG(self):
		if self.pauseFlag == True:
			self.pauseFlag = False
		else:
			self.pauseFlag = True
		
	def drawBlack(self, x, y):
		self.w.create_rectangle(x, y, 
							x+10, y+10,
							outline="white", fill="black")
	def drawWhite(self, x,y):
		self.w.create_rectangle(x, y, 
							x+10, y+10,
							outline="white", fill="white")

	def key(self, event):
		if event.char and event.char in self.dirc:
			#print self.dirn[event.char], self.dirn[self.direction]
			if self.dirn[event.char] + self.dirn[self.direction] != 0:
				self.direction = event.char

	def task(self):
		if self.pauseFlag:
			self.w.after(500)
			self.master.after(150, self.task)
			return
	
		if self.score == self.winNum:	
			#print 'You win!'
			self.w.create_text(200, 200, text="You Win")
			return
		self.sk.tail = list(self.sk.body[-1])
		self.sk.body_follow()
		self.sk.move[self.direction.lower()](self.ledge, self.redge, self.tedge, self.bedge)


		if not self.swl:
			self.score += 1
			self.var.set(self.score)
			self.pie.makeFood(self.sk, self.redge, self.bedge)
			self.drawBlack(self.pie.x, self.pie.y)
			self.swl = True
		
		self.drawWhite(self.sk.tail[0], self.sk.tail[1])
		self.drawBlack(self.sk.body[0][0], self.sk.body[0][1])

		if self.sk.body[0][0] == self.pie.x and self.sk.body[0][1] == self.pie.y:
			self.swl = False
			self.sk.body.append(list(self.sk.tail))
			self.sk.l += 1
			self.drawBlack(self.sk.body[-1][0], self.sk.body[-1][1])
		#self.w.after(900)
		#self.w.update()
		self.master.after(150, self.task)

App()